package com.yangwudi.service.impl;

import com.yangwudi.anno.LogOperation;
import com.yangwudi.exception.DeptHasEmpsException;
import com.yangwudi.mapper.DeptMapper;
import com.yangwudi.mapper.EmpMapper;
import com.yangwudi.pojo.Dept;
import com.yangwudi.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class DeptServiceImpl implements DeptService {
    @Autowired
    private DeptMapper deptMapper;
    @Autowired
    private EmpMapper empMapper;

    @Override
    public List<Dept> findAll() {

        return deptMapper.findAll();
    }

//    @LogOperation
    @Override
    public void deleteById(Integer id) {
        int empCount = empMapper.getEmpCountByDeptId(id);
        if (empCount > 0){
            throw new DeptHasEmpsException("对不起，当前部门下有员工，不能直接删除");
        }
        deptMapper.deleteById(id);

    }

//    @LogOperation
    @Override
    public void add(Dept dept) {
        //1. 补全基础属性 - createTime, updateTime
        dept.setCreateTime(LocalDateTime.now());
        dept.setUpdateTime(LocalDateTime.now());

        //2. 调用mapper接口方法插入数据
        deptMapper.insert(dept);
    }

    @Override
    public Dept getById(Integer id) {
        return deptMapper.getById(id);
    }

    @Override
    public void update(Dept dept) {
        //1. 补全基础属性 - updateTime
        dept.setUpdateTime(LocalDateTime.now());
        //2. 调用mapper接口方法修改数据
        deptMapper.update(dept);
    }
}
